package com.matt.controller.pad;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.courseV2.GrowthObservation;
import com.matt.domain.courseV2.GrowthObservationRecord;
import com.matt.domain.courseV2.po.GrowthObservationCountPO;
import com.matt.domain.courseV2.po.GrowthObservationRecordAllPO;
import com.matt.domain.courseV2.po.GrowthObservationRecordPO;
import com.matt.domain.courseV2.po.MilepostV2PO;
import com.matt.domain.courseV2.vo.GrowthObservationRecordVO;
import com.matt.foundation.DO.ChildrenNurseryListDO;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.CopyUtils;
import com.matt.service.ChildrenService;
import com.matt.service.courseV2.GrowthObservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.matt.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/pad/milepost_record")
@Slf4j
@Api(tags = "成长观察v2 - 学生记录")
public class PadGrowthObservationRecordController {

    @Autowired
    private GrowthObservationService growthObservationService;

    @Autowired
    protected MemVerificationCache cache;

    @Autowired
    private ChildrenService childrenService;

    @ApiOperation(value = "学生成长记录 - 列表")
    @PostMapping("/getGrowthList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "classId", value = "班级ID", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "学生名", required = false, dataType = "string"),
    })
    public StatusResponse getGrowthList(@RequestHeader(TICKET) String ticket, String classId, String name, PagerQuery pagerQuery) {
        String areaId = cache.getAreaId(ticket);
        Page<GrowthObservationCountPO> list = growthObservationService.getGrowthList(areaId,classId,name,pagerQuery);
        return StatusResponse.success(list);
    }

    @ApiOperation(value = "学生成长 - 月配置")
    @PostMapping("/getMonthAgeGrowth")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "monthAge", value = "月龄", required = false, dataType = "int"),
    })
    public StatusResponse getMonthAgeGrowth(Integer monthAge) {
        GrowthObservation getMonthAgeGrowth   = growthObservationService.getMonthAgeGrowth(monthAge);
        return StatusResponse.success(getMonthAgeGrowth);
    }

    @ApiOperation(value = "学生成长 - 记录")
    @PostMapping("/getGrowthChildrenList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "childrenId", value = "学生ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "classId", value = "班级ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "monthAge", value = "月龄", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "states", value = "状态：0未完成，1完成", required = false, dataType = "string"),
    })
    public StatusResponse getGrowthChildrenList(@RequestHeader(TICKET) String ticket,String childrenId,String classId,Integer monthAge,String states, PagerQuery pagerQuery) {
        Page<GrowthObservationRecordVO>  list = growthObservationService.getGrowthChildrenList(childrenId,classId,monthAge,states,pagerQuery);
        return StatusResponse.success(list);
    }


    @ApiOperation(value = "学生成长记录 - 添加")
    @PostMapping("/add")
    public StatusResponse add(@RequestHeader(TICKET) String ticket,GrowthObservationRecordAllPO growthObservationRecord) {
        StaffDTO staff = cache.getStaff(ticket);
        growthObservationService.addAll(staff, growthObservationRecord,staff.getRealName());
        return StatusResponse.success("添加成功");
    }

    @ApiOperation(value = "里程碑 - 列表")
    @PostMapping("/getMileageList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
    })
    public StatusResponse getMileageList(@RequestHeader(TICKET) String ticket, PagerQuery pagerQuery) {
        String areaId = cache.getAreaId(ticket);
        List<MilepostV2PO>  list = growthObservationService.getMileageList(areaId,pagerQuery);
        return StatusResponse.success(list);
    }

    @ApiOperation(value = "里程碑 - 生成小卡")
    @PostMapping("/addUrl")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "recordId", value = "记录ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "url", value = "卡图片路径", required = true, dataType = "string"),
    })
    public StatusResponse addUrl(String recordId, String  url) {
        GrowthObservationRecord growthObservationRecord  = growthObservationService.update(recordId,url);
        return StatusResponse.success(growthObservationRecord);
    }


    @ApiOperation(value = "里程碑 - 详情")
    @PostMapping("/get")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "recordId", value = "记录ID", required = true, dataType = "string"),
    })
    public StatusResponse get(String recordId) {
        GrowthObservationRecord growthObservationRecord  = growthObservationService.getId(recordId);
        GrowthObservationRecordPO po = new GrowthObservationRecordPO();
        CopyUtils.copyProperties(growthObservationRecord,po);
        po.setChildren(childrenService.getById(po.getChildrenId()));
        return StatusResponse.success(po);
    }
}
